package com.learn.controller;

import com.learn.entity.Person;
import com.learn.service.PoiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
/**
 * 导出
 */
public class ReportController {
    @Autowired
    HttpServletResponse response;

    @Autowired
    PoiService poiService;

    @RequestMapping("/report/excel")
    public Object reportExcel() {
        Integer dataNum = 10;
        return poiService.export(response,dataNum);
    }
    /**
     * 导出excel-easyexcel(alibaba)
     * @return
     */
    @RequestMapping("/report/output/excel")
    public Object reportOutPutExcel() {
        /*ExcelWriter writer = null;
        OutputStream out = null;
        try {
            String fileName = "测试";
            out = response.getOutputStream();
            writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
            Sheet sheet1 = new Sheet(1, 0,Person.class);
            sheet1.setSheetName("第一个sheet");
            writer.write(dataCenter(), sheet1);
            writer.finish();
            response.setContentType("multipart/form-data");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment;filename="+fileName+".xlsx");
            response.setHeader("FileName", fileName+".xlsx");
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }*/

        return null;
    }

    public List<Person>  dataCenter() {
        List<Person> dataInfo = new ArrayList<>();
        Person person1 = new Person();
        person1.setName("aaa");
        person1.setSex(1);
        person1.setAge(16);
        dataInfo.add(person1);

        Person person2 = new Person();
        person2.setName("bbb");
        person2.setSex(2);
        person2.setAge(18);
        dataInfo.add(person2);
        return dataInfo;
    }
}
